<?php

namespace App\Http\Requests\Shop;

use Illuminate\Foundation\Http\FormRequest;

class ProductStore extends FormRequest
{
   /**
    * Determine if the user is authorized to make this request.
    *
    * @return bool
    */
   public function authorize()
   {
      return true;
   }

   /**
    * Get the validation rules that apply to the request.
    *
    * @return array
    */
   public function rules()
   {
      return [
         'category_id' => 'required',
         'name' => 'required|max:100',
         'price' => 'required',
         'stock' => 'required',
         'image' => 'required',
         'description' => 'required',
      ];
   }

   public function messages()
   {
      return [
         'category_id.required' => '分类必须选择',
         'name.required' => '商品名称必须填',
         'name.max' => '商品名称最大不能超过100个字符',
         'price.required' => '商品价格不能为空',
         'stock.required' => '商品库存不能为空',
         'image.required' => '缩略图必须上传',
         'description.required' => '商品描述不能为空',
      ];
   }

   // woo 改变验证后的默认行为
   public function failedValidation(\Illuminate\Contracts\Validation\Validator $validator)
   {
      exit(json_encode(array(
         'status' => 0,
         'msg' => $validator->getMessageBag()->first()
      )));
   }
}
